<Project Sdk="Microsoft.Build.NoTargets">

  <!--
    Add basic project properties for NuGet restore, needed to import the SourceLink MSBuild tool
    package's targets into the build.
  -->
  <Import Project="$(RepositoryEngineeringDir)versioning.targets" />

  <PropertyGroup>
    <IncrementalNativeBuild Condition="'$(IncrementalNativeBuild)' == ''">true</IncrementalNativeBuild>
    <BuildCoreHostDependsOn>GetProductVersions;GenerateRuntimeVersionFile</BuildCoreHostDependsOn>
    <BuildCoreHostDependsOn Condition="'$(EnableSourceControlManagerQueries)' == 'true'">$(BuildCoreHostDependsOn);InitializeSourceControlInformationFromSourceControlManager</BuildCoreHostDependsOn>
    <IntermediateOutputRootPath>$(ArtifactsObjDir)$(OutputRID).$(Configuration)\</IntermediateOutputRootPath>
    <!-- Global location for version files -->
    <NativeVersionFile Condition="$([MSBuild]::IsOsPlatform(Windows))">$(ArtifactsObjDir)_version.h</NativeVersionFile>
    <NativeVersionFile Condition="!$([MSBuild]::IsOsPlatform(Windows))">$(ArtifactsObjDir)_version.c</NativeVersionFile>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="$(CoreClrProjectRoot)tools/InjectResource/InjectResource.csproj" ReferenceOutputAssembly="false" OutputItemType="InjectResourceTool"/>
  </ItemGroup>

  <Target Name="PrepareSingleFileHostWithEmbeddedDacPrereqs">
    <PropertyGroup>
      <SingleFileHostPath>$(CoreCLRArtifactsPath)/corehost/singlefilehost$(ExeSuffix)</SingleFileHostPath>
      <DacPath>$(CoreCLRArtifactsPath)/mscordaccore$(LibSuffix)</DacPath>
      <SingleFileHostDestinationPath>$(DotNetHostBinDir)/singlefilehost$(ExeSuffix)</SingleFileHostDestinationPath>
    </PropertyGroup>
    <PropertyGroup Condition="'$(TargetOS)' == 'windows'">
      <!-- Symbols for Windows are in a PDB subdirectory -->
      <SingleFileHostSymbolsPath>$(CoreCLRArtifactsPath)/corehost/PDB/singlefilehost$(SymbolsSuffix)</SingleFileHostSymbolsPath>
      <SingleFileHostSymbolsDestinationPath>$(DotNetHostBinDir)/PDB/singlefilehost$(SymbolsSuffix)</SingleFileHostSymbolsDestinationPath>
    </PropertyGroup>
    <PropertyGroup Condition="'$(TargetOS)' != 'windows'">
      <!-- Symbols for non-Windows are next to the binary -->
      <SingleFileHostSymbolsPath>$(CoreCLRArtifactsPath)/corehost/singlefilehost$(SymbolsSuffix)</SingleFileHostSymbolsPath>
      <SingleFileHostSymbolsDestinationPath>$(DotNetHostBinDir)/singlefilehost$(SymbolsSuffix)</SingleFileHostSymbolsDestinationPath>
    </PropertyGroup>
  </Target>

  <!--
    Target that copies the singlefilehost and embeds the DAC into it.
    We embed the DAC now instead of during the native build because we need to sign the DAC and signing the DAC during CMake would be ugly.
  -->
  <Target Name="PrepareSingleFileHostWithEmbeddedDacCore"
    Inputs="$(SingleFileHostPath);
            $(SingleFileHostSymbolsPath);
            $(DacPath);
            @(InjectResourceTool)"
    Outputs="$(SingleFileHostDestinationPath);$(SingleFileHostSymbolsDestinationPath)"
    DependsOnTargets="ResolveProjectReferences"
    Condition="'$(RuntimeFlavor)' != 'Mono' and
               Exists('$(SingleFileHostPath)')">
    <Copy SourceFiles="$(SingleFileHostPath)" DestinationFiles="$(SingleFileHostDestinationPath)" />
    <Exec Condition="'$(TargetOS)' == 'windows'"
          Command="&quot;$(DotNetTool)&quot; exec @(InjectResourceTool) --bin &quot;$(DacPath)&quot; --image &quot;$(SingleFileHostDestinationPath)&quot; --name MINIDUMP_EMBEDDED_AUXILIARY_PROVIDER" />

    <!-- Copy symbols if they exist. We may not have separate symbols if they were not stripped from the native binary itself. -->
    <Copy SourceFiles="$(SingleFileHostSymbolsPath)" DestinationFiles="$(SingleFileHostSymbolsDestinationPath)" Condition="Exists('$(SingleFileHostSymbolsPath)')" />
  </Target>

  <Target Name="PrepareSingleFileHostWithEmbeddedDac"
          DependsOnTargets="PrepareSingleFileHostWithEmbeddedDacPrereqs;PrepareSingleFileHostWithEmbeddedDacCore"
          AfterTargets="Build"/>

  <!-- Target that builds dotnet, hostfxr and hostpolicy with the same version as what NetCoreApp will be built for
       since the build produced artifacts should always version the same (even if they may not get used).
  -->
  <Target Name="BuildCoreHostUnix"
          Condition="'$(TargetOS)' != 'windows'"
          AfterTargets="Build"
          DependsOnTargets="$(BuildCoreHostDependsOn)">
    <PropertyGroup>
      <CMakeBuildDir>$(IntermediateOutputRootPath)corehost\cmake\</CMakeBuildDir>
      <BuildScript>$([MSBuild]::NormalizePath('$(MSBuildThisFileDirectory)', 'build.sh'))</BuildScript>

      <_CoreHostUnixTargetOS>$(TargetOS)</_CoreHostUnixTargetOS>
      <_CoreHostUnixTargetOS Condition="'$(TargetsLinuxBionic)' == 'true'">linux-bionic</_CoreHostUnixTargetOS>
      <BuildArgs>$(Configuration) $(TargetArchitecture) -commithash "$([MSBuild]::ValueOrDefault('$(SourceRevisionId)', 'N/A'))" -os $(_CoreHostUnixTargetOS)</BuildArgs>
      <BuildArgs>$(BuildArgs) -cmakeargs "-DVERSION_FILE_PATH=$(NativeVersionFile)"</BuildArgs>
      <BuildArgs Condition="'$(ConfigureOnly)' == 'true'">$(BuildArgs) -configureonly</BuildArgs>
      <BuildArgs Condition="'$(PortableBuild)' != 'true'">$(BuildArgs) -portablebuild=false</BuildArgs>
      <BuildArgs Condition="'$(KeepNativeSymbols)' != 'false'">$(BuildArgs) -keepnativesymbols</BuildArgs>
      <BuildArgs Condition="'$(CrossBuild)' == 'true'">$(BuildArgs) -cross</BuildArgs>
      <BuildArgs Condition="'$(Compiler)' != ''">$(BuildArgs) $(Compiler)</BuildArgs>
      <BuildArgs Condition="'$(CMakeArgs)' != ''">$(BuildArgs) -cmakeargs "$(CMakeArgs)"</BuildArgs>
      <BuildArgs Condition="'$(Ninja)' == 'true'">$(BuildArgs) -ninja</BuildArgs>
      <BuildArgs>$(BuildArgs) -runtimeflavor $(RuntimeFlavor)</BuildArgs>
      <BuildArgs Condition="'$(EnableNativeSanitizers)' != ''">$(BuildArgs) -fsanitize=$(EnableNativeSanitizers)</BuildArgs>
      <BuildArgs Condition="'$(OfficialBuildId)' != ''">$(BuildArgs) /p:OfficialBuildId="$(OfficialBuildId)"</BuildArgs>
      <BuildArgs>$(BuildArgs) -outputrid $(OutputRID)</BuildArgs>
    </PropertyGroup>

    <!--
      Use IgnoreStandardErrorWarningFormat because Arcade sets WarnAsError and there's an existing
      warning in the macOS build when dsymutil tries to strip symbols.
    -->
    <Message Text="&quot;$(BuildScript)&quot; $(BuildArgs)" Importance="High"/>
    <Exec Command="&quot;$(BuildScript)&quot; $(BuildArgs)" IgnoreStandardErrorWarningFormat="true"/>
  </Target>

  <Target Name="BuildCoreHostWindows"
          Condition="'$(TargetOS)' == 'windows'"
          AfterTargets="Build"
          DependsOnTargets="$(BuildCoreHostDependsOn)">
    <!-- Generate Version files -->
    <ItemGroup>
      <HostFiles Include="dotnet">
       <FileDescription>.NET Host</FileDescription>
      </HostFiles>
      <HostFiles Include="hostfxr">
        <FileDescription>.NET Host Resolver - $(HostResolverVersion)</FileDescription>
      </HostFiles>
      <HostFiles Include="hostpolicy">
        <FileDescription>.NET Host Policy - $(HostPolicyVersion)</FileDescription>
      </HostFiles>
      <HostFiles Include="comhost">
        <FileDescription>.NET COM Host</FileDescription>
      </HostFiles>
      <HostFiles Include="ijwhost">
        <FileDescription>.NET IJW Host</FileDescription>
      </HostFiles>
      <HostFiles Include="nethost">
        <FileDescription>.NET Component Host</FileDescription>
      </HostFiles>
    </ItemGroup>

    <MSBuild
      Projects="$(MSBuildProjectFullPath)"
      Properties="
        GenerateNativeVersionInfo=true;
        AssemblyName=%(HostFiles.FileDescription);
        NativeVersionFile=$(IntermediateOutputRootPath)hostResourceFiles\%(HostFiles.Identity)\version_info.h"
      Targets="GenerateNativeVersionFile"
      Condition="
        '$(IncrementalNativeBuild)' != 'true' or
        !Exists('$(IntermediateOutputRootPath)hostResourceFiles\%(HostFiles.Identity)\version_info.h')"/>

    <PropertyGroup>
      <BuildScript>$([MSBuild]::NormalizePath('$(MSBuildThisFileDirectory)', 'build.cmd'))</BuildScript>

      <BuildArgs>$(Configuration) $(TargetArchitecture) commit $([MSBuild]::ValueOrDefault('$(SourceRevisionId)', 'N/A')) outputrid $(OutputRID)</BuildArgs>
      <BuildArgs Condition="'$(ConfigureOnly)' == 'true'">$(BuildArgs) configureonly</BuildArgs>
      <BuildArgs Condition="'$(PortableBuild)' == 'true'">$(BuildArgs) portable</BuildArgs>
      <BuildArgs Condition="'$(IncrementalNativeBuild)' == 'true'">$(BuildArgs) incremental-native-build</BuildArgs>
      <BuildArgs>$(BuildArgs) rootdir $(RepoRoot)</BuildArgs>
      <BuildArgs Condition="'$(Ninja)' == 'false'">$(BuildArgs) msbuild</BuildArgs>
      <BuildArgs>$(BuildArgs) runtimeflavor $(RuntimeFlavor)</BuildArgs>
      <BuildArgs>$(BuildArgs) runtimeconfiguration $(RuntimeConfiguration)</BuildArgs>
      <BuildArgs Condition="'$(EnableNativeSanitizers)' != ''">$(BuildArgs) -fsanitize=$(EnableNativeSanitizers)</BuildArgs>
    </PropertyGroup>

    <!--
      Run script that invokes Cmake to create VS files, and then calls msbuild to compile them. Use
      IgnoreStandardErrorWarningFormat because Arcade sets WarnAsError and there's an existing
      warning in the native build.
    -->
    <Message Text="&quot;$(BuildScript)&quot; $(BuildArgs)" Importance="High"/>
    <Exec Command="&quot;$(BuildScript)&quot; $(BuildArgs)" IgnoreStandardErrorWarningFormat="true"/>
  </Target>

  <Target Name="PrependWindowsHeaderIncludeToVersionHeaderFile"
          Condition="'$(TargetOS)' == 'windows'"
          AfterTargets="GenerateNativeVersionFile">
    <PropertyGroup>
      <IncludeStatementLine>#include &lt;Windows.h&gt;</IncludeStatementLine>
      <NativeVersionFileContents>$([System.IO.File]::ReadAllText('$(NativeVersionFile)'))</NativeVersionFileContents>
    </PropertyGroup>

    <WriteLinesToFile
      File="$(NativeVersionFile)"
      Lines="$(IncludeStatementLine);$(NativeVersionFileContents)"
      Overwrite="true" />
  </Target>

  <Import Project="$(RepositoryEngineeringDir)nativeSanitizers.targets" />
</Project>
